import axiosIns, { axiosOdata } from './../utils/axios';
import axios from 'axios';
import update, { updateChain } from 'immutability-helper-x';
import { data, recomdData, latestData, distinctData } from '../utils/mock';
/**
|--------------------------------------------------
| Constants
|--------------------------------------------------
*/
const GET_BANNER = 'GET_BANNER';
const GET_RECOMD = 'GET_RECOMD'; //获取推荐
const GET_LATEST = 'GET_LATEST'; //最新推荐
const GET_DISTINCT = 'GET_DISTINCT'; //目的地

/**
|--------------------------------------------------
| Actions
|--------------------------------------------------
*/
export function getBanner() {
  return async (dispatch, getState) => {
    const { home, user } = getState();
    const response = await axiosIns('/getBannerList');
    const { data } = response;
    dispatch({
      type: GET_BANNER,
      payLoad: data.list
    });
  };
}

export function getRecomdList() {
  return async (dispatch, getState) => {
    const { home, user } = getState();
    if (!user.userInfo || !user.userInfo.token) return;
    const { token } = user.userInfo;
    const response = await axiosIns('/getRecomdList');
    const { data } = response;
    dispatch({
      type: GET_RECOMD,
      payLoad: data.list
    });
  };
}

export function getLatestList() {
  return async (dispatch, getState) => {
    const { home, user } = getState();
    // if (!user.userInfo || !user.userInfo.token) return;
    // const { token } = user.userInfo;
    const response = await axiosIns('/getLatestList');
    const { data } = response;
    dispatch({
      type: GET_LATEST,
      payLoad: data.list
    });
  };
}

export function getDistinctList() {
  return async (dispatch, getState) => {
    const { home, user } = getState();
    // if (!user.userInfo || !user.userInfo.token) return;
    // const { token } = user.userInfo;
    const response = await axiosIns('/getDistinctList');
    const { data } = response;
    dispatch({
      type: GET_DISTINCT,
      payLoad: data.list
    });
  };
}
/**
|--------------------------------------------------
| Action Handlers
|--------------------------------------------------
*/
const ACTION_HANDLERS = {
  [GET_BANNER]: (state, action) => {
    return update.$set(state, 'banner.bannerList', action.payLoad);
  },
  [GET_RECOMD]: (state, action) => {
    return update.$set(state, 'recomd.recomdList', action.payLoad);
  },
  [GET_LATEST]: (state, action) => {
    return update.$set(state, 'latest.latestList', action.payLoad);
  },
  [GET_DISTINCT]: (state, action) => {
    return update.$set(state, 'distinct.distinctList', action.payLoad);
  }
};

/**
|--------------------------------------------------
| Reducer
|--------------------------------------------------
*/
const initialState = {
  banner: {
    bannerList: []
  },
  recomd: {
    recomdList: []
  },
  latest: {
    latestList: []
  },
  distinct: {
    distinctList: []
  }
};

export default function(state = initialState, action) {
  const handler = ACTION_HANDLERS[action.type];
  return handler ? handler(state, action) : state;
}
